<?php

namespace common\components\behaviors;

use yii\base\Behavior;
use common\models\BaseSuppGoodsSite;
use common\models\YdSyncLog;


class BaseSuppGoodsSiteBehavior extends Behavior{
	public function events() {
		return [
			BaseSuppGoodsSite::EVENT_GOODS_SITE_STATUS => 'updateSiteStatus',
		    BaseSuppGoodsSite::EVENT_UPDATE_STATUS => 'saveStatus',
		];
	}
	
	public function updateSiteStatus($event) {
		$goods = $this->owner;
		$data  = $goods->changeModels;
		$status = $data['status'];
		$goods_id = $data['supplier_goods_id'];
		
		if(in_array($data['site_id'], [1,2,4,14,16,17,19,24,25,27,28,30,33,29,34])){
			$site = f_p('site_params_other')[$data['site_id']];

			if($data['status']==1){
				$status = 1;
			}else{
				$status = 0;
			}
			
			$post_data = [
							'token'=> md5(HBID.CONFUSE.date('Ymd', time())),
							'hbid' => HBID,
							'act'=>2,
							'site' => $site,
							'sku'=>$goods_id,
							'status' => $status
					     ];
			
			$result = http_post_data(YD_URL, $post_data);
// 			YdSyncLog::insertLog($result, $goods, 7);
			
		}	
	}
	
	
	/**
	 * @description:修改状态
	 * @return: return_type
	 * @author: leo
	 * @date: 2015年7月24日下午3:54:14
	 * @modified_date: 2015年7月24日下午3:54:14
	 * @modified_user: leo
	 * @review_user:
	*/
	public function  saveStatus($event){
	    $goods = $this->owner;

	    if(in_array($goods->site_id, [1,2,4,14,16,17,19,24,25,27,28,30,33,29,34])){
	        $site = f_p('site_params_other')[$goods->site_id];
	        
	        if($goods->status==1){
	            $status = 1;
	        }else{
	            $status = 0;
	        }
	        
	        $post_data = [
	            'token'=> md5(HBID.CONFUSE.date('Ymd', time())),
	            'hbid' => HBID,
	            'act'=>2,
	            'site' => $site,
	            'sku'=>$goods->supplier_goods_id,
	            'status' => $status
	        ];
	        
	        	
	        $result = http_post_data(YD_URL, $post_data);
// 	        YdSyncLog::insertLog($result, $goods, 7);
	        
	    }
	}
}

?>